import log from '../../components/logPop/logPop'
import store from '../../store/store'
import router from '../../router/index'
export default {
  install (Vue, options) {
    Vue.prototype.$logPop = function (data) {
      let POP = Vue.extend(log)
      let promise = new Promise((resolve, reject) => {
        let template = new POP({
          data () {
            return {
              isLogin: data,
              status: false,
              validateError: false,
              loginErrorStr: ''
            }
          },
          methods: {
            hide () {
              template.$el.remove()
            },
            login (data) {
              store.dispatch('user/login', data).then((response) => {
                if (response.code === '200') {
                  resolve(response)
                  template.$el.remove()
                } else {
                  this.validateError = true
                  this.loginErrorStr = response.msg
                }
              })
            },
            loginError (data) {
              if (data !== true) {
                this.validateError = true
                this.loginErrorStr = data
              }
            },
            register () {
              this.switchFn()
            },
            switchFn () {
              this.status = true
              this.isLogin = !this.isLogin
              setTimeout(() => {
                this.status = false
              }, 500)
            },
            goRester () {
              template.$el.remove()
              router.push({name: 'resetPassWord'})
            }
          }
        }).$mount(document.createElement('div'))
        document.body.appendChild(template.$el)
      })
      return promise
    }
  }
}
